<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\Member;

class MemberController extends Controller
{
    //展示会员信息视图

    public function index(){

  		$data = Member::get();
        


    	return view('admin.member.index',compact('data'));


    }
    // 插入会员数据
    public function add(Request $request){

    	if ($request->isMethod('post')) {

    		$result = Member::insert([

    			'username'  => $request->input('username'),
                'password'  => bcrypt('654321'),
                'gender'    => $request->input('gender'),
                'mobile'    => $request->input('mobile'),
                'email'     => $request->input('email'),
                'avatar'    => $request->input('avatar'),
                'province_id'=> $request->input('province'),
                'city_id'=> $request->input('city'),
                'county_id'=> $request->input('area'),
                'type'      =>$request->input('type'),
                'status'    =>$request->input('status'),
                'created_at' =>date('Y-m-d H:i:s')

    		]);
            return $result ? '1' : '0';

    		
    	}else{
    		return view('admin.member.add');

    	}

    	
    }
    //显示单个会员信息
     public function show(Request $request){

        
        //解析json 地址信息
        $json_string = file_get_contents('./admin/list.json');
        $addrr = json_decode($json_string, true);
        

        $data = Member::where('id',$request->only('id'))->get();
         // print_r($data[0]->province_id);
        
        $province = isset($addrr[$data[0]['province_id']])?$addrr[$data[0]['province_id']]:'';
        $city = isset($addrr[$data[0]['city_id']])? $addrr[$data[0]['city_id']]:'';
        $county = isset($addrr[$data[0]['county_id']])?$addrr[$data[0]['county_id']]:'';
        // dd($province,$city,$county);
        
        
        

    	return view('admin.member.show',compact('data','province','city','county'));
    }
    //停用会员
    public function stop(Request $request){
        // $id = $_GET['id'];

        $data = Member::where('id',$request->only('id'))->update(['status' => '2']);
    }

    //启用会员
    public function start(Request $request){
        

        $data = Member::where('id',$request->only('id'))->update(['status' => '1']);
    }
    //更新会员数据
    public function edit(Request $request){
        

        if ($request->isMethod('post')) {
            

            $result = Member::where('id',$request->input('user_id'))->update([

                'username'  => $request->input('username'),
                'gender'    => $request->input('gender'),
                'mobile'    => $request->input('mobile'),
                'email'     => $request->input('email'),
                'avatar'    => $request->input('avatar'),
                'province_id'=> $request->input('province'),
                'city_id'=> $request->input('city'),
                'county_id'=> $request->input('area'),
                'type'      =>$request->input('type'),
                'status'    =>$request->input('status'),
                'updated_at' =>date('Y-m-d H:i:s')

            ]);
            return $result ? '1' : '0';
        }else{
            
            

            $data = Member::where('id',$request->only('id'))->get();

            $province = isset($addrr[$data[0]['province_id']])?$addrr[$data[0]['province_id']]:'';
            $city = isset($addrr[$data[0]['city_id']])? $addrr[$data[0]['city_id']]:'';
            $county = isset($addrr[$data[0]['county_id']])?$addrr[$data[0]['county_id']]:'';

            return view('admin.member.edit',compact('data','province','city','county'));
        }


    }
  
  
  
  
}
